<template>
  <el-dialog
    v-model="visible"
    title="新增投坯"
    width="800px"
    :before-close="handleClose"
  >
    <el-form :model="form" label-width="120px">
      <el-row>
        <el-form-item label="计划编号">
          <el-input readonly v-model="form.plan_id" />
        </el-form-item>
        <el-form-item label="计划产量">
          <el-input readonly v-model="form.plan_num" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="已投产量">
          <el-input readonly v-model="form.group_total" />
        </el-form-item>
        <el-form-item label="剩余产量">
          <el-input readonly v-model="form.plan_remainder" />
        </el-form-item>
      </el-row>
       <el-row>
        <el-form-item label="产品客户">
          <el-input readonly v-model="form.cus_name" />
        </el-form-item>
        <el-form-item label="产品规格">
          <el-input readonly v-model="form.std_name" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="产品类型">
          <el-input readonly :model-value="ddo.text('prodTypes',form.plan_type)" />
        </el-form-item>
        <el-form-item label="产品花色">
          <el-input readonly v-model="form.color_name" />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="投坯产量">
          <el-input-number
            v-model="form.group_num"
            :min="1"
            :max="form.plan_remainder"
            controls-position="right"
            :precision="1"
            :step="0.1"
          />
        </el-form-item>
        
      </el-row>
      <!-- <el-row>
        <el-form-item label="计划编号">
          <el-input readonly v-model="form.plan_id" />
        </el-form-item>
        <el-form-item label="计划产量">
          <el-input readonly v-model="form.plan_id" />
        </el-form-item>
      </el-row>
        <el-form-item label="计划月份">
          <el-select v-model="form.plan_month">
            <el-option
              :label="item"
              :value="item"
              v-for="item in yearMonthList"
              :key="item"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="产品类型">
          <el-select v-model="form.plan_type">
            <el-option label="请选择" value=""></el-option>
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in ddo.prodTypes"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item label="产品客户">
          <el-select v-model="form.plan_cus">
            <el-option label="请选择" value=""></el-option>
            <el-option
              :label="item.info_name"
              :value="item.info_id"
              v-for="item in cusList"
              :key="item.info_id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="产品规格">
          <el-select v-model="form.plan_std">
            <el-option label="请选择" value=""></el-option>
            <el-option
              :label="item.info_name"
              :value="item.info_id"
              v-for="item in stdList"
              :key="item.info_id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-row>

      <el-row>
        <el-form-item label="产品花色">
          <el-select v-model="form.plan_color">
            <el-option label="全部" value=""></el-option>
            <el-option
              :label="item.info_name"
              :value="item.info_id"
              v-for="item in colorList"
              :key="item.info_id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="计划产量">
          <el-input-number
            v-model="form.plan_num"
            :min="0"
            controls-position="right"
            :precision="1"
            :step="0.1"
          />
        </el-form-item>
      </el-row>
      <el-form-item label="计划序号">
        <el-input-number
          ref="xhRef"
          v-model="form.plan_xh"
          :min="0"
          controls-position="right"
          :precision="0"
          :step="1"
        />
      </el-form-item> -->
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" @click="exec">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup >
import { reactive, ref, computed } from "vue";
import { tip, get, post, put, del, ddo } from "@/common";

//获取年月列表
const yearMonthList = ref({});
const getYearMonthData = () => {
  get("/plan/manager/year-month-list").then((result) => {
    form.plan_month = result.data.nextYearMonth;
    const currentYearMonth = result.data.currentYearMonth;
    yearMonthList.value = result.data.list.filter(
      (item) => item >= currentYearMonth
    );
    console.log(yearMonthList.value);
  });
};

//获取基础信息
const infoList = ref([]);
const cusList = computed(() =>
  infoList.value.filter((item) => item.info_type === 1)
);
const stdList = computed(() =>
  infoList.value.filter((item) => item.info_type === 2)
);
const colorList = computed(() =>
  infoList.value.filter((item) => item.info_type === 3)
);
const getInfoList = () => {
  get("/plan/manager/info").then((result) => {
    infoList.value = result.data;
  });
};

const visible = ref(false);
const open = (item) => {
  for (let prop in item) {
    form[prop] = item[prop];
  }
  getYearMonthData();
  getInfoList();
  visible.value = true;
};
const handleClose = (done) => {
  done();
};

const form = reactive({
  plan_id: "",
  plan_xh: 0,
  plan_month: "", //计划月份
  plan_cus: "",
  plan_std: "",
  plan_color: "",
  plan_num: 0, //计划数量
  plan_type: "", // 产品类型
  plan_date: "", //计划下达时间
  plan_person: "", //计划下达人
  plan_status: "", //计划是否下达
  group_num:0,
});

const emit = defineEmits(["ok"]);
const exec = () => {
  post("/process/group", form, {
    title: "提示",
    message: "您确定新增投坯吗？",
  }).then((result) => {
    emit("ok");
    visible.value = false;
    tip.success("投坯成功！");
  });
};

defineExpose({ open });
</script>

<style>
</style>